/**
  ******************************************************************************
  * File Name          : ctrl_common.h
  * Description        : Code for data process applications
  ******************************************************************************
  * @attention
  *
  ******************************************************************************
  */

#ifndef __ctrl_common_H
#define __ctrl_common_H

#include <stdint.h>
#include <string.h>
#include "bsp.h"
#include "route_ec_interface.h"
#include "route_dk_interface.h"

#define CTR_STORAGE_MD_KEY           "CTRPMD"
#define CTR_STORAGE_JT_KEY           "CTRPJT"
#define CTR_STORAGE_DL1_KEY          "CTRDL1"
#define CTR_STORAGE_DL2_KEY          "CTRDL2"
#define CTR_STORAGE_SK_KEY           "CTRPSK"
#define CTR_STORAGE_TD_KEY           "CTRPTD"
#define CTR_STORAGE_DK_KEY           "CTRPDK"

#define CTRVFD_QMSG_LENGTH          (CTR_VFD_CHANNEL_ALLNUM * 4)
#define CTRVFD_QMSG_ITEMSIZE        sizeof(CtrVfd_QmsgType)

typedef struct
{
	volatile uint8_t  fan_tc_recont;
	volatile uint8_t  shl_tc_recont;
	volatile uint8_t  nuq_tc_recont;
	volatile uint8_t  vfd_tc_recont[CTR_VFD_CHANNEL_ALLNUM];
	
	volatile uint8_t  fan_tc_ticker_en;
	volatile uint8_t  shl_tc_ticker_en;
	volatile uint8_t  nuq_tc_ticker_en;
	volatile uint8_t  vfd_tc_ticker_en[CTR_VFD_CHANNEL_ALLNUM];
	
	volatile uint32_t fan_tc_ticker;
	volatile uint32_t shl_tc_ticker;
	volatile uint32_t nuq_tc_ticker;
	volatile uint32_t vfd_tc_ticker[CTR_VFD_CHANNEL_ALLNUM];
}Ctr_TcTickersType;

typedef struct
{
	uint8_t vfdid;
	uint8_t level;
}CtrVfd_QmsgType;

typedef struct
{
  char key[8];
  Ctrl_ParaMDType para;
}Ctrl_StorageMDType;

typedef struct
{
  char key[8];
  Ctrl_ParaJTType para;
}Ctrl_StorageJTType;

typedef struct
{
  char key[8];
  Ctrl_ParaDL1Type para;
}Ctrl_StorageDL1Type;

typedef struct
{
  char key[8];
  Ctrl_ParaDL2Type para;
}Ctrl_StorageDL2Type;

typedef struct
{
  char key[8];
  Ctrl_ParaSKType para;
}Ctrl_StorageSKType;

typedef struct
{
  char key[8];
  Ctrl_ParaTDType para;
}Ctrl_StorageTDType;

typedef struct
{
  char key[8];
  Ctrl_ParaDKType para;
}Ctrl_StorageDKType;

extern void Ctr_InitState(void);

extern void Ctr_TCTimerInit(void);

extern void Ctr_SaveMDPara(void);
extern void Ctr_SaveJTPara(void);
extern void Ctr_SaveDL1Para(void);
extern void Ctr_SaveDL2Para(void);
extern void Ctr_SaveSKPara(void);
extern void Ctr_SaveTDPara(void);
extern void Ctr_SaveDKPara(void);

extern uint8_t Ctr_LoadMDPara(void);
extern uint8_t Ctr_LoadJTPara(void);
extern uint8_t Ctr_LoadDL1Para(void);
extern uint8_t Ctr_LoadDL2Para(void);
extern uint8_t Ctr_LoadSKPara(void);
extern uint8_t Ctr_LoadTDPara(void);
extern uint8_t Ctr_LoadDKPara(void);

extern void Control_CreateTask(void);

extern volatile Ctrl_StateFNType Ctrl_StateFN, Ctrl_StateFN_last;
extern volatile Ctrl_StateSLType Ctrl_StateSL, Ctrl_StateSL_last;
extern volatile Ctrl_StateNQType Ctrl_StateNQ, Ctrl_StateNQ_last;
extern volatile Ctrl_StateVFType Ctrl_StateVF, Ctrl_StateVF_last;
extern volatile Ctrl_StateDKType Ctrl_StateDK, Ctrl_StateDK_last;

extern Ctrl_ParaMDType  Ctrl_ParaMD;
extern Ctrl_ParaJTType  Ctrl_ParaJT;
extern Ctrl_ParaDL1Type Ctrl_ParaDL1;
extern Ctrl_ParaDL2Type Ctrl_ParaDL2;
extern Ctrl_ParaSKType  Ctrl_ParaSK;
extern Ctrl_ParaTDType  Ctrl_ParaTD;
extern Ctrl_ParaDKType  Ctrl_ParaDK;

extern volatile Ctr_TcTickersType Ctr_TcTickers;

extern void Ctrl_dk(Sys_DataTimeType *tm);
extern void Ctrl_xc(int16_t temper);
extern void Ctrl_zd(void);
extern void Ctrl_bj(int16_t temper);
extern void Ctrl_fan_nh3(uint8_t nh3);

extern void Ctrl_fan_mode_jt(int16_t temper);
extern void Ctrl_fan_mode_dl(int16_t temper, uint32_t ticker, const volatile uint32_t *vf_ticker);
extern void Ctrl_fan_mode_sk(int16_t temper, uint32_t ticker, const volatile uint32_t *vf_ticker);
extern void Ctrl_fan_mode_zn(int16_t temper, uint32_t ticker, const volatile uint32_t *vf_ticker);

extern void Ctrl_shl_mode_jt(int16_t temper);
extern void Ctrl_shl_mode_dl(int16_t temper, uint32_t ticker);
extern void Ctrl_shl_mode_sk(int16_t temper, uint32_t ticker);
extern void Ctrl_shl_mode_zn(int16_t temper, uint32_t ticker);

extern void Ctrl_nuq_mode_jt(int16_t temper);
extern void Ctrl_nuq_mode_dl(int16_t temper, uint32_t ticker);
extern void Ctrl_nuq_mode_sk(int16_t temper, uint32_t ticker);
extern void Ctrl_nuq_mode_zn(int16_t temper, uint32_t ticker);


extern void CtrFan_SetActuator(volatile uint8_t act, uint8_t ch, uint8_t mu);
extern void CtrShl_SetActuator(volatile uint8_t act, uint8_t ch, uint8_t mu);
extern void CtrNuq_SetActuator(volatile uint8_t act, uint8_t ch, uint8_t mu);
extern void CtrBj_SetActuator(volatile uint8_t act);
extern void CtrZd_SetActuator(volatile uint8_t act);
extern void CtrDk_SetActuator(volatile uint8_t act, uint8_t ch);
extern void CtrXc_SetActuator(volatile uint8_t act);


extern uint8_t CtrFan_GetActuator(uint8_t ch);
extern uint8_t CtrShl_GetActuator(uint8_t ch);
extern uint8_t CtrNuq_GetActuator(uint8_t ch);
extern void CtrVfd_Actuator(volatile uint8_t act, uint8_t ch, uint8_t mu);
extern uint8_t CtrDk_GetActuator(uint8_t ch);

extern uint8_t CtrFan_GetActNum(void);

extern void CtrFan_TimeCtrlISR(void);
extern void CtrShl_TimeCtrlISR(void);
extern void CtrNuq_TimeCtrlISR(void);
extern void CtrVfd_TimeCtrlISR(void);

extern void Ctrl_RefreshStateFN(uint8_t force);
extern void Ctrl_RefreshStateSL(uint8_t force);
extern void Ctrl_RefreshStateNQ(uint8_t force);
extern void Ctrl_RefreshStateVF(uint8_t force);
extern void Ctrl_RefreshStateDK(uint8_t force);

extern volatile uint8_t zd_act;
extern volatile uint8_t bj_act;
extern volatile uint8_t fn_act[CTR_FAN_CHANNEL_ALLNUM];
extern volatile uint8_t sl_act[CTR_SHL_CHANNEL_ALLNUM];
extern volatile uint8_t nq_act[CTR_NUQ_CHANNEL_ALLNUM];
extern volatile uint8_t vf_act[CTR_VFD_CHANNEL_ALLNUM];
extern volatile uint8_t vf_act_last[CTR_VFD_CHANNEL_ALLNUM];
extern volatile uint8_t dk_act[DKCTR_PT_CHANNEL_ALLNUM];
extern volatile uint8_t xc_act;                              /* 小窗为子设备统一执行, 若后期需要分立执行 可改为数组 */

extern volatile uint8_t Ctrl_BjTrigger;

#endif /* __ctrl_common_H */
